Skip to main content

Python Naming Conventions

For a more detailed look at python standards look at PEP8

If anyone has any questions about Python standards that can't be answered from this document, googling your question with keyword pep8 might be helpful, or you can reach out to Jake.

Keywords

Here are some important keywords to remember:

  • camelCase
  • PascalCase
  • snake_case or lower_snake_case
  • SNAKE_CASE or UPPER_SNAKE_CASE
  • kebab-case

More info on these naming cases can be found on Free Code Camp

Variable Naming Conventions

All variables in Python should be written in snake_case or UPPER_SNAKE_CASE.

Standard Variables

Variable names should be descriptive and lower_snake_case.

mainstem_river_mile = river_mile_array[0]
tributary_river_mile = river_mile_array[1]

Constants

A constant is a value that is the same through an entire program and can not be changed.

It is best practice to define all constants in a single block near the top of a file. Constants should be defined after imports but before any class or function declarations, unless the constant is defined inside a class.

Constants should be written in UPPER_SNAKE_CASE.

import foo

MAX_OVERFLOW = 100


def moo():
return 0

Single-letter Variables

In most cases, using single-letter variables is not acceptable. The following exceptions exist:

It's okay to use a single-letter variable in loops:

an_array = [1, 2, 3]

for i in an_array:
print(i)

There are also more grey-areas where they might be used: n for number, c for count, and single-letter measurements or units such as m (meters), l (liters), k (kelvin), etc.

Using single-letter variables when trying to replicate equations or scientific formulae (similar to measurement letters) is also allowed.

Function Declarations

Similar to vanilla variables, Python functions (or methods) should be lower_snake_case and descriptive.

def add_values(x, y):
return x + y

Class Declarations

Class declarations in Python are unique. They should follow PascalCase, for example:

class RiverStation():

def get_name(self):
return self.name

File Names

Similar to vanilla variables and function declarations, Python files should be lower_snake_case and descriptive.

gqc-repo-python/
- manage.py
- setup_cross_sections.py
- insert_daily_flow_history.py

Indentation

At GQC, we use 2 spaces per indentation level. Indentation is VERY IMPORTANT in Python. If your indentation is off, then your code won't work.